package cn.falcon.fmobi.console.controller;

import cn.falcon.fmobi.admin.entity.ShiroUser;
import cn.falcon.fmobi.biz.service.CustomerService;
import cn.falcon.fmobi.biz.vo.DomainUser;
import cn.falcon.fmobi.biz.vo.FmobiUser;
import cn.falcon.fmobi.console.constant.UserRolePermission;
import cn.falcon.fmobi.console.exception.CallBackException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;

public class BaseCtrl {

    @Autowired
    protected CustomerService customerService;

    /**
     * 获取广告主用户信息
     *
     * @return
     */
    public FmobiUser readUserSession(Integer advertiserId) {

        if (hasRole(UserRolePermission.USER_ROLE_ADVERTISER)) {
            return (FmobiUser) SecurityUtils.getSubject().getSession().getAttribute("fmobiUser");
        } else {
            if (advertiserId == null) {
                throw new CallBackException(29999, "缺少广告主ID参数");
            }
            return customerService.readCustomerToUser(advertiserId);
        }

    }

    /**
     * 获取当前登录用户的信息
     *
     * @return
     */
    public FmobiUser readSession(HttpSession session) {
        FmobiUser user = (FmobiUser) SecurityUtils.getSubject().getSession().getAttribute("fmobiUser");
        if (hasRole(UserRolePermission.USER_ROLE_AGENT)) {
            Integer advertiserId = getLoginUser(session) != null ? getLoginUser(session).getAdvertiserId() : null;
            if (advertiserId != null) {
                user = customerService.readCustomerToUser(advertiserId);
                user.setRole(UserRolePermission.USER_ROLE_ADVERTISER);
                return user;
            } else {
                user.setRole(UserRolePermission.USER_ROLE_AGENT);
            }
        } else if (hasRole(UserRolePermission.USER_ROLE_ADVERTISER)) {
            user.setRole(UserRolePermission.USER_ROLE_ADVERTISER);
        } else {
            user.setRole(UserRolePermission.USER_ROLE_PLATFORM);
        }
        return user;

    }

    /**
     * 验证用户是否包含角色 roleName
     *
     * @return
     */
    public boolean hasRole(String... roleNames) {
        for (String roleName : roleNames) {
            if (SecurityUtils.getSubject().hasRole(roleName))
                return true;
        }
        return false;
    }

    /**
     * 获取当前登录用户的信息
     *
     * @return
     */
    public ShiroUser getCurrentUser() {
        return SecurityUtils.getSubject().getPrincipals().oneByType(ShiroUser.class);
    }

    /**
     * 判断用户是否具体roleName 的角色，有的话，返回该登录用户 对应 关系表的id
     *
     * @param roleName
     * @return
     */
    public Integer getActorId(String roleName) {
        if (hasRole(roleName))
            return getCurrentUser().getActorId();
        return null;
    }

    public void validateRoles(String... roleNames) {
        if (!hasRole(roleNames)) {
            throw new AuthorizationException();
        }
    }

    public DomainUser getLoginUser(HttpSession session) {
        DomainUser domainUser = (DomainUser) session.getAttribute("loginUser");
        return domainUser != null ? domainUser : null;
    }

    /**
     * 获取用户溢价比例
     *
     * @param session
     * @return
     */
    public BigDecimal readUserRates(HttpSession session) {
        if (hasRole(UserRolePermission.USER_ROLE_AGENT)) {
            Integer advertiserId = getLoginUser(session) != null ? getLoginUser(session).getAdvertiserId() : null;
            if (advertiserId != null) {
                return readUserSession(advertiserId).getRebate();
            }
        }
        FmobiUser user = (FmobiUser) SecurityUtils.getSubject().getSession().getAttribute("fmobiUser");
        return user.getRebate();
    }
}
